package network;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class MultiChatClient extends JFrame implements Runnable{

	Socket socket ;
	MultiChatClientThread mcct;
	StringBuilder data = new StringBuilder();
	Vector<String> clientList = new Vector<>();
	
	private JPanel contentPane;
	private JPanel panel;
	private JPanel panel_1;
	public  JButton btnDisconnect;
	public  JButton btnConnect;
	public  JTextField message;
	public  JList list;
	public  JTextField userID;
	public  JComboBox comboBox;
	public  JButton btnSend;
	public  JButton btnFileSend;

	private JPanel panel_2;
	private JButton btnNewButton_3;
	private JScrollPane scrollPane;
	private JLabel lblNewLabel;
	private JScrollPane scrollPane_1;
	private JTextPane result;
	private JPanel panel_3;
	private JLabel lblNewLabel_1;
	private JTextField serverIP;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JPanel panel_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					MultiChatClient frame = new MultiChatClient();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MultiChatClient() {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				serverDisconnect();
			}
		});
		setMinimumSize(new Dimension(520, 390));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 587, 390);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getPanel(), BorderLayout.NORTH);
		contentPane.add(getPanel_1(), BorderLayout.SOUTH);
		contentPane.add(getPanel_2(), BorderLayout.WEST);
		contentPane.add(getScrollPane_1(), BorderLayout.CENTER);
		
		try{
			InetAddress ia = InetAddress.getLocalHost();
			String title = "서버IP:" + ia.getHostAddress();
			title += " / Port : 4444";
			setTitle(title);
			serverIP.setText(ia.getHostAddress());
		}catch(Exception ex){
			
		}
	}
	
	public void serverConnect(){
		Thread t = new Thread(this);
		t.start();
	}

	@Override
	public void run() {
		try{
			socket = new Socket(serverIP.getText(), 4444);
			mcct = new MultiChatClientThread(this, socket);
			mcct.start();
			
			btnConnect.setEnabled(false);
			
			btnDisconnect.setEnabled(true);
			btnSend.setEnabled(true);
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	

	public void serverDisconnect(){
		btnConnect.setEnabled(true);
		
		btnDisconnect.setEnabled(false);
		btnSend.setEnabled(false);
		
		SocketData sd = 
			new SocketData("logout", userID.getText(),
					" 님이 로그아웃 하셨네요" ,null);
		mcct.sendMsg(sd);
	}

	public void sendMsg(){
		SocketData sd = new SocketData("message", userID.getText(),message.getText(),null);
		//dataSet(sd);
		mcct.sendMsg(sd);
	}
	
	public void sendFile(){
		try{
			String filename = null;
			JFileChooser fc = new JFileChooser();
			int yn = fc.showOpenDialog(null);
			if(yn == JFileChooser.APPROVE_OPTION){	
				filename =  fc.getSelectedFile().toString();
			}
			SocketData sd = new SocketData("file", "server", "파일이 전송되었습니다.",filename);
			mcct.sendMsg(sd);
		}catch(Exception ex){}

	}
	
	public void dataSet(SocketData sd){
		String icon = "";
		if(sd.getIndex() >0){
			icon = "<img src='file:/p:/workspace/과정진행/1403/1403/imo" 
					+ sd.getIndex() + ".gif' width='100' height='100'><br/>";
			data.append(icon);
		}
		
		data.append(sd.getUser() + " : " + sd.getMessage() + "<br/>");
		result.setText(data.toString());
		
		result.setCaretPosition(result.getDocument().getLength());
		
	}
	
	
	
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(new BorderLayout(0, 0));
			panel.add(getPanel_3(), BorderLayout.EAST);
		}
		return panel;
	}
	private JButton getBtnDisconnect() {
		if (btnDisconnect == null) {
			btnDisconnect = new JButton("종료");
			btnDisconnect.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					serverDisconnect();
				}
			});
		}
		return btnDisconnect;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getMessage(), BorderLayout.CENTER);
			panel_1.add(getComboBox(), BorderLayout.WEST);
			panel_1.add(getPanel_4(), BorderLayout.EAST);
		}
		return panel_1;
	}
	private JTextField getMessage() {
		if (message == null) {
			message = new JTextField();
			message.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent ev) {
					if(ev.getKeyCode() == KeyEvent.VK_ENTER){
						sendMsg();
					}
				}
			});
			message.setColumns(10);
		}
		return message;
	}
	private JComboBox getComboBox() {
		if (comboBox == null) {
			comboBox = new JComboBox();
			comboBox.setPreferredSize(new Dimension(60, 21));
			
			comboBox.addItem(new ImageIcon("imo0.png"));
			comboBox.addItem(new ImageIcon("imo1.png"));
			comboBox.addItem(new ImageIcon("imo2.png"));
			comboBox.addItem(new ImageIcon("imo3.png"));
			comboBox.addItem(new ImageIcon("imo4.png"));
			comboBox.addItem(new ImageIcon("imo5.png"));
			comboBox.addItem(new ImageIcon("imo6.png"));
			comboBox.addItem(new ImageIcon("imo7.png"));
			comboBox.addItem(new ImageIcon("imo8.png"));
			comboBox.addItem(new ImageIcon("imo9.png"));
			comboBox.addItem(new ImageIcon("imo10.png"));

			comboBox.addItem(new ImageIcon("imo11.png"));
			comboBox.addItem(new ImageIcon("imo12.png"));
			comboBox.addItem(new ImageIcon("imo13.png"));
			comboBox.addItem(new ImageIcon("imo14.png"));
			comboBox.addItem(new ImageIcon("imo15.png"));
			comboBox.addItem(new ImageIcon("imo16.png"));
			comboBox.addItem(new ImageIcon("imo17.png"));
			comboBox.addItem(new ImageIcon("imo18.png"));
			
		}
		return comboBox;
	}
	private JPanel getPanel_2() {
		if (panel_2 == null) {
			panel_2 = new JPanel();
			panel_2.setPreferredSize(new Dimension(160, 10));
			panel_2.setLayout(new BorderLayout(0, 0));
			panel_2.add(getBtnNewButton_3(), BorderLayout.SOUTH);
			panel_2.add(getScrollPane(), BorderLayout.CENTER);
		}
		return panel_2;
	}
	private JButton getBtnNewButton_3() {
		if (btnNewButton_3 == null) {
			btnNewButton_3 = new JButton("귓속말 해제");
		}
		return btnNewButton_3;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getList());
			scrollPane.setColumnHeaderView(getLblNewLabel());
		}
		return scrollPane;
	}
	private JList getList() {
		if (list == null) {
			list = new JList();
		}
		return list;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("접속자");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel;
	}
	private JScrollPane getScrollPane_1() {
		if (scrollPane_1 == null) {
			scrollPane_1 = new JScrollPane();
			scrollPane_1.setViewportView(getResult());
			scrollPane_1.setColumnHeaderView(getLblNewLabel_1());
		}
		return scrollPane_1;
	}
	private JTextPane getResult() {
		if (result == null) {
			result = new JTextPane();
			result.setEditable(false);
			result.setContentType("text/html");
		}
		return result;
	}
	private JPanel getPanel_3() {
		if (panel_3 == null) {
			panel_3 = new JPanel();
			panel_3.add(getLblNewLabel_2());
			panel_3.add(getTextField_3());
			panel_3.add(getLblNewLabel_3());
			panel_3.add(getTextField_2_1());
			panel_3.add(getBtnConnect());
			panel_3.add(getBtnDisconnect());
		}
		return panel_3;
	}
	private JButton getBtnConnect() {
		if (btnConnect == null) {
			btnConnect = new JButton("접속");
			btnConnect.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					serverConnect();
				}
			});
		}
		return btnConnect;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("메시지");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNewLabel_1;
	}
	private JTextField getTextField_2_1() {
		if (userID == null) {
			userID = new JTextField();
			userID.setText("park");
			userID.setColumns(10);
		}
		return userID;
	}
	private JTextField getTextField_3() {
		if (serverIP == null) {
			serverIP = new JTextField();
			serverIP.setColumns(10);
		}
		return serverIP;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("서버주소");
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("아이디");
		}
		return lblNewLabel_3;
	}
	private JPanel getPanel_4() {
		if (panel_4 == null) {
			panel_4 = new JPanel();
			panel_4.add(getBtnSend());
			panel_4.add(getBtnFileSend());
		}
		return panel_4;
	}
	private JButton getBtnSend() {
		if (btnSend == null) {
			btnSend = new JButton("전송");
			btnSend.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					sendMsg();
				}
			});
		}
		return btnSend;
	}
	private JButton getBtnFileSend() {
		if (btnFileSend == null) {
			btnFileSend = new JButton("파일전송");
			btnFileSend.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					SocketData sd = 
						new SocketData("fileTransPort", 
							userID.getText(), "파일 전송하려고요 ^^", null);
					mcct.sendMsg(sd);
				}
			});
		}
		return btnFileSend;
	}
}
